package com.example.tse.rbyair.secondclassiify;

import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.os.Looper;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.example.tse.rbhttpservice.service.RemoteServiceFactory;
import com.example.tse.rbhttpservice.service.RemoteServiceListenerT;
import com.example.tse.rbhttpservice.service.common.model.CommonGetADList;
import com.example.tse.rbhttpservice.service.common.model.CommonGetADListRequest;
import com.example.tse.rbhttpservice.service.common.model.CommonGetADListResponse;
import com.example.tse.rbhttpservice.service.goods.model.GoodsGetVirtualCatInfoList;
import com.example.tse.rbhttpservice.service.goods.model.GoodsGetVirtualCatInfoProducts;
import com.example.tse.rbhttpservice.service.goods.model.GoodsGetVirtualCatInfoRequest;
import com.example.tse.rbhttpservice.service.goods.model.GoodsGetVirtualCatInfoResponse;
import com.example.tse.rbyair.R;
import com.example.tse.rbyair.widget.GridViewWithHeaderAndFooter;
import com.example.tse.rbyair.widget.imagecycleview.ADInfo;
import com.example.tse.rbyair.widget.imagecycleview.ImageCycleView;

import org.kymjs.kjframe.adapter.Common2TypeAdapter;
import org.kymjs.kjframe.adapter.ViewHolder;
import org.kymjs.kjframe.log.L;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tse on 16/1/26.
 */
public class SecondClassifyActivity extends FragmentActivity {


    private com.example.tse.rbyair.widget.GridViewWithHeaderAndFooter reclassifylist;
    private Common2TypeAdapter<GoodsGetVirtualCatInfoProducts> listAdapter;


    private ArrayList<GoodsGetVirtualCatInfoProducts> produstListData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        
        L.d("SecondClassifyActivity onCreate getClipboardText:" +getClipboardText());


        setContentView(R.layout.activity_main_secondclassify);
        produstListData = new ArrayList<>();

        reclassifylist = (GridViewWithHeaderAndFooter) findViewById(R.id.reclassify_list);


        View header = LayoutInflater.from(this).inflate(R.layout.list_header_secondclassify, null);
        final ImageCycleView cycleImageView = (ImageCycleView) header.findViewById(R.id.reclassify_mainiv);
        cycleImageView.pushImageCycle();


        reclassifylist.addHeaderView(header);

        listAdapter = new Common2TypeAdapter<GoodsGetVirtualCatInfoProducts>(this, produstListData, R.layout.list_item_second_classify, R.layout.list_item_second_classify) {

            @Override
            public int getViewTypeCount() {
                return 2;
            }

            @Override
            public int getItemViewType(int position) {
                return 0;
            }

            @Override
            public void convert(ViewHolder holder, GoodsGetVirtualCatInfoProducts product, int position) {


                ImageView imageView = holder.getView(R.id.iv_img);
//                DisplayImageOptions options = new DisplayImageOptions.Builder()
//                        .cacheInMemory(true)
//                        .cacheOnDisk(true)
//                        .bitmapConfig(Bitmap.Config.RGB_565)
//                        .build();

//                ImageLoader.getInstance().displayImage( product.getThumbnailPic(), imageView, options);
//                ImageLoader.getInstance().displayImage(product.getThumbnailPic(), imageView);


                holder.setPicassoImageResource(R.id.iv_img, R.drawable.occupying_goodscontent, R.drawable.occupying_goodscontent, product.getThumbnailPic());
                holder.setText(R.id.saleNameTxt, product.getName());
                holder.setText(R.id.salePriceTxt, product.getPrice());
                holder.setText(R.id.oldSalePriceTxt, product.getMktprice());
            }

            @Override
            public void convertType2(ViewHolder holder, GoodsGetVirtualCatInfoProducts product, int position) {
                holder.setPicassoImageResource(R.id.iv_img, R.drawable.occupying_goodscontent, R.drawable.occupying_goodscontent, product.getThumbnailPic());

                holder.setText(R.id.saleNameTxt, product.getName());
            }
        };
        reclassifylist.setAdapter(listAdapter);


        GoodsGetVirtualCatInfoRequest virtualCatInforrequest = new GoodsGetVirtualCatInfoRequest() {
            @Override
            public String giveVirtualCatId() {
                return "19";
            }
        };
        RemoteServiceFactory.getInstance().getGoodsService(getApplication()).getVirtualCatInfo(virtualCatInforrequest, new RemoteServiceListenerT<GoodsGetVirtualCatInfoResponse>() {
            @Override
            public void okData(GoodsGetVirtualCatInfoResponse goodsGetVirtualCatInfoResponse) {
                super.okData(goodsGetVirtualCatInfoResponse);

                Boolean isMAinThread = Looper.myLooper() == Looper.getMainLooper();
                L.d("SecondClassifyActivity isMAinThread:" + isMAinThread);
                final List<GoodsGetVirtualCatInfoProducts> produstList = goodsGetVirtualCatInfoResponse.getProducts();
                L.d("SecondClassifyActivity produstList size:" + produstList.size());


                for (int i = 0; i < produstList.size(); i++) {
                    GoodsGetVirtualCatInfoProducts produc = produstList.get(i);

                    L.d("SecondClassifyActivity produc getName:" + produc.getName() + " getPrice:" + produc.getPrice());
                }


                produstListData.clear();
                produstListData.addAll(produstList);
                listAdapter.notifyDataSetChanged();


                List<GoodsGetVirtualCatInfoList> goodList = goodsGetVirtualCatInfoResponse.getList();
                L.d("SecondClassifyActivity goodList.size():" + goodList.size());

            }
        });


        CommonGetADListRequest adListRequest = new CommonGetADListRequest() {
            @Override
            public String givePosition() {
                return "14";
            }

            @Override
            public String giveVirtualCatId() {
                return "19";
            }
        };
        RemoteServiceFactory.getInstance().getCommonService(getApplication()).getADList(adListRequest, new RemoteServiceListenerT<CommonGetADListResponse>() {
            @Override
            public void PreStart() {
                super.PreStart();
            }

            @Override
            public void okData(CommonGetADListResponse commonGetADListResponse) {
                super.okData(commonGetADListResponse);

                ArrayList<ADInfo> AdInforList = new ArrayList<ADInfo>();


                List<CommonGetADList> adList = commonGetADListResponse.getGetADList();
//                L.d("SecondClassifyActivity adList.size():" + adList.size());
                for (int i = 0; i < adList.size(); i++) {
                    CommonGetADList ad = adList.get(i);

                    ADInfo adinfor = new ADInfo();
                    adinfor.setUrl(ad.getImg());
                    adinfor.setType(ad.getBannerType());
                    AdInforList.add(adinfor);
                }


//                cycleImageView.setImageResources(AdInforList, new ImageCycleView.ImageCycleViewListener() {
//                    @Override
//                    public void displayImage(String imageURL, ImageView imageView) {
//                        Picasso.with(getApplication()).load(imageURL).into(imageView);
//                    }
//
//                    @Override
//                    public void onImageClick(ADInfo info, int postion, View imageView) {
//                        L.d("SecondClassifyActivity ImageCycleViewListener postion:" + postion);
//                    }
//                });


            }
        });




    }

    private String getClipboardText() {


        
        
        ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        String text = "";
        try {
            if (clipboard != null && clipboard.hasPrimaryClip()) {
                CharSequence tmpText = clipboard.getText();
                clipboard.setText(tmpText);
                if (tmpText != null && tmpText.length() > 0) {
                    text = tmpText.toString().trim();
                }
            }
        } catch (Exception e) {
            L.d("SecondClassifyActivity e:"+e);
            e.printStackTrace();
            text = "";
        }
        return text;
    }
}
